/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.proteu.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author eros
 */
public class RegexUtils {


         static Pattern pattern; //
         static Matcher matcher;

    public static String getStringFromFileBySeparator(String fileName,String charaterSeparator1,String charaterSeparator2){

         String s = IOUtils.getStringFromFile(fileName);
      
        return getStringFromStringBySeparator(s, charaterSeparator1,charaterSeparator2);
    }

    public static String getStringFromStringBySeparator(String stringStream,String charaterSeparator1,String charaterSeparator2){

          CharSequence s = stringStream;//IOUtils.getStringFromFile(fileName);
          pattern = Pattern.compile("\\"+charaterSeparator1+"((\\s*?.*?)*?)\\"+charaterSeparator2+"");


                StringBuffer sb = new StringBuffer();
		matcher = pattern.matcher(s);

		while (matcher.find()) {

                        String email = matcher.group();

                        if(email != null && email.length()>2){
                            email = email.substring(1,email.length()-1);
                        
                            sb.append(email+"\n");
                        }

		}
		
        return sb.toString();

    }

/**
  * @param String
     * @return Long number of tags <br> and <t*>
    */


    public static Long tagsCounterDefault(String stringStream){
        Long brTags = tagsCounter(stringStream, "<br", ">");
        Long tTags = tagsCounter(stringStream, "<t", ">");
        //Long tTags = tagsCounter(stringStream, "<t", ">");
        return brTags+tTags;
    }

    public static Long tagsCounter(String stringStream,String charaterSeparator1,String charaterSeparator2){

        //System.out.println(stringStream.length());
        //stringStream = stringStream.substring(0, 9000);

         CharSequence s = stringStream;//IOUtils.getStringFromFile(fileName);
         pattern = Pattern.compile("\\"+charaterSeparator1+"((\\s*?.*?)*?)\\"+charaterSeparator2+"");

            //long i = 0;
            Long y = new Long(0);
                StringBuffer sb = new StringBuffer();
		matcher = pattern.matcher(s);

		while (matcher.find()) {
                        y ++;

		}

        return y;
    }

    @Deprecated
    public static String replaceAllParameters(String stringStream,List<String> listValues,List<String> listParameters){
        
//         Pattern pattern; //
//         Matcher matcher;
        if(listValues != null && listParameters != null && listValues.size() == listParameters.size() && stringStream != null){
                for(int i = 0; i < listParameters.size(); i ++){
                        pattern = Pattern.compile(listParameters.get(i));
                        matcher = pattern.matcher(stringStream);
                        stringStream = matcher.replaceAll(listValues.get(i));
                }
        }

        return stringStream;
    }




    public static String replaceAllParameters(String stringStream,Map<String,String> mapItems){

            CharSequence sequenceChar = stringStream.subSequence(0, stringStream.length());
            pattern = Pattern.compile("\\<#((\\s*?.*?)*?)\\>");
            matcher = pattern.matcher(sequenceChar);
            StringBuffer myStringBuffer = new StringBuffer();

            int tagLength = 0;
            String strItemKey ;
            String tag ;
            while(matcher.find()){
                tag = matcher.group();
                if(tag != null && tag.length()>1){
                    
                    tagLength = tag.length();
                    strItemKey = tag.substring(2,tagLength-1);
                    String item = mapItems.get(strItemKey);
                    //System.out.println("strItemKey: "+strItemKey+" - item: "+item );
                    if(item != null){
                         matcher.appendReplacement(myStringBuffer, item);
                    }
                }

            }
            matcher.appendTail(myStringBuffer);
            
        return myStringBuffer.toString();
    }


//     public static String replaceAllParametersTest(String stringStream,Map<String,String> mapItems){
//            System.out.println("Inside replaceAllParametersTest");
//            CharSequence sequenceChar = stringStream.subSequence(0, stringStream.length());
//            pattern = Pattern.compile("\\<#((\\s*?.*?)*?)\\>");
//            matcher = pattern.matcher(sequenceChar);
//            StringBuffer myStringBuffer = new StringBuffer();
//
//            int tagLength = 0;
//            String strItemKey ;
//            String tag ;
//            while(matcher.find()){
//                tag = matcher.group();
//                if(tag != null && tag.length()>1){
//
//                    tagLength = tag.length();
//                    strItemKey = tag.substring(2,tagLength-1);
//                    String item = mapItems.get(strItemKey);
//                    System.out.println("strItemKey: "+strItemKey+" - item: "+item );
//                    if(item != null){
//                         matcher.appendReplacement(myStringBuffer, item);
//                    }
//                }
//
//            }
//            matcher.appendTail(myStringBuffer);
//
//        return myStringBuffer.toString();
//    }

public static String spaceLessAndUpperCase(String nome){

    List<String> list ;
    String strResut = null;

    if(nome != null && nome.trim().length()>2){

        pattern = Pattern.compile("[,\\s]+");
        list = new ArrayList<String>();
        //nome = nome.trim();
        strResut = "";
        String[] result = pattern.split(nome);

                for (int i=0; i<result.length; i++){
                    //System.out.println(result[i]);
                    list.add(result[i]);
                }

                for(int i = 0; i <list.size(); i ++){
                    if(i < list.size()){
                        strResut = strResut+list.get(i)+" ";
                    }else{
                        strResut = strResut+list.get(i);
                    }
                }
                strResut = strResut.toUpperCase().trim();
            //System.out.println(nome);
    }


        return strResut;

    }

//25783526x
public static String rgFormater(String rg){
    if(rg != null && rg.length()>=4){
        rg = rg.trim().toUpperCase();
        String ultimoDigito  = null;
        if(rg.length()>8){
            ultimoDigito = rg.substring(rg.length()-1, rg.length());
            rg = rg.substring(0, rg.length()-1);
        }
        rg = rg.replaceAll("\\D", "");

        if(ultimoDigito != null){
            return rg+ultimoDigito;
        }else{
            return rg;
        }
        
        
    }
    return null;
}




public static List<Integer> retornaHorasMinutos(String s){
    List<Integer> list = new ArrayList<Integer>(2);
    if(s != null && s.length()>2){
        String[] splitString = (s.split("\\:+"));

		for (String string : splitString) {
			if(string != null){
                            list.add(new Integer(string));
                        }

		}
    }
		if(list != null && list.size()==2){
                    Integer intHr = new Integer(list.get(0));
                    Integer intMn = new Integer(list.get(1));
                    if(intHr < -1 ){
                        list.set(0, 0);
                    }else if(intHr>24){
                        list.set(0, 24);
                    }
                    if(intMn < -1 ){
                        list.set(1, 0);
                    }else if(intMn >59){
                        list.set(1, 59);
                    }

                }
		
    return list;
}


        public  static List<String> geradorDeHrs(String strHrStart,String strHrEnd,String strIntervalo){

            List<String> listReturn;

            if(strHrStart != null && strHrEnd != null && strIntervalo != null){

                Integer start = new Integer(strHrStart.replaceAll("\\D", ""));
                Integer end = new Integer(strHrEnd.replaceAll("\\D", ""));
                Integer intervalo = new Integer(strIntervalo.replaceAll("\\D", ""));

                            List<Integer> listStart = retornaHorasMinutos(strHrStart);
                            List<Integer> listEnd = retornaHorasMinutos(strHrEnd);

                            if(listStart != null && listStart.size()==2 && listEnd != null && listEnd.size()==2){

                                    Calendar cal =  new GregorianCalendar();
                                    Calendar calStart =  new GregorianCalendar(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),listStart.get(0),listStart.get(1),0);
                                    Calendar calEnd =  new GregorianCalendar(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),listEnd.get(0),listEnd.get(1),0);
        

                                    if(start != null && start>0&& end != null && end>start&& intervalo != null && intervalo>9){

                                        Integer lapsoTemp = end-start;
                                        //System.out.println("lapsoTemp: "+lapsoTemp);
                                        //Calendar calControle = new GregorianCalendar();
                                        //calControle.setTime(calStart.getTime());
                                        DateFormat format = new SimpleDateFormat("HH:mm");
                                        if(lapsoTemp>=intervalo){
                                            listReturn = new ArrayList<String>();
                                            //Integer controle = start;
                                            do{
                                                
                                                listReturn.add(format.format(calStart.getTime()));
                                                calStart.add(Calendar.MINUTE, intervalo);
                                                if(calEnd.equals(calStart)){
                                                    listReturn.add(format.format(calStart.getTime()));
                                                    break;
                                                }

                                            }while(calEnd.after(calStart));
                                            
                                                
                                                
                                            return listReturn;
                                        }
                                    }



                }

            }

            return null;
        }


}
